package com.qf.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.qf.entity.Goods;
import com.qf.entity.ResultEntity;
import com.qf.feign.api.IGoodsService;
import com.qf.feign.api.ISearchGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping(value = "/goods")
@Slf4j
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private ISearchGoodsService searchGoodsService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Value("${exchange}")
    private String exchange;

    @RequestMapping(value = "/addGoods")
    @ResponseBody
    public ResultEntity addGoods(Goods goods) {

        // 1.添加商品的数据库
        ResultEntity resultEntity = goodsService.addGoods(goods);

        // 2.添加商品的es
        if(ResultEntity.SUCEESS.equals(resultEntity.getStatus())){
            try {
                Map map = (Map)resultEntity.getData();
                if(map != null){
                    map.get("id");
                    goods.setId(Integer.parseInt(map.get("id").toString()));
                    // 3.添加MQ中
                    rabbitTemplate.convertAndSend(exchange,"goods.add",goods);
                }
            }catch (Exception e){
                log.error("添加商品的队列中失败:"+goods,e);
            }

        }

        // 3.生成商品的寄静态页面
        return resultEntity;
    }

    @RequestMapping(value = "/getGoodsPage")
    public String getGoodsPage(Page<Goods> page, Model model){

        // 1.查询MySQL
//        ResultEntity resultEntity = goodsService.getGoodsPage(page);

        // 2.查询ES
        ResultEntity resultEntity = searchGoodsService.getGoodsPage(page);

        model.addAttribute("page",resultEntity.getData());
        model.addAttribute("url","http://localhost/shop-back/goods/getGoodsPage");
        return "goods/goodsList";
    }
}
